package admin

import (
	"gitee.com/kordar/layuiadmin/admin/inject"
	"gitee.com/kordar/layuiadmin/admin/middleware"
	"github.com/gin-gonic/gin"
	digstarter "github.com/kordar/dig-starter"
	ginsysstarter "github.com/kordar/ginsys-starter"
	middleware2 "github.com/kordar/ginsys/middleware"
	"github.com/kordar/ginsys/resource"
	"gitlab.com/go-box/pongo2gin/v6"
	"go.uber.org/dig"
	"net/http"
)

func routerProvider() {
	digstarter.ProvideE(func() (func(router *gin.Engine), error) {
		return Router, nil
	}, dig.Group(ginsysstarter.RouterIocGroup.Value()))
}

func Router(router *gin.Engine) {

	router.HTMLRender = pongo2gin.Default()
	router.StaticFS("/statics", http.Dir("./statics"))
	router.StaticFS("/templates", http.Dir("./templates"))

	router.Use(middleware2.RecoveryMiddleware())

	group := router.Group("/")
	digstarter.InvokeE(func(p inject.RouterParams) {
		router.GET("/login", p.AuthCtrl.Index)
		router.GET("/index", p.HomeCtrl.Index)
		router.GET("/", p.HomeCtrl.Index)
		router.GET("/admin", p.AdminCtrl.Index)
		router.NoRoute(p.HomeCtrl.NoFound)

		router.POST("/login/captcha", p.AuthCtrl.Captcha)
		router.GET("/login/captcha", p.AuthCtrl.Captcha)
		router.POST("/login/submit", p.AuthCtrl.Login)

		group.Use(middleware.AdminAuth(p.AdminRepos))
		{
			group.POST("/resource/:apiName/info", resource.GetInfo)
			group.POST("/resource/:apiName/list", resource.GetList)
			group.POST("/resource/:apiName/add", resource.Add)
			group.POST("/resource/:apiName/update", resource.Update)
			group.POST("/resource/:apiName/delete", resource.Delete)
			group.POST("/resource/:apiName/edit", resource.Edit)
			group.POST("/resource/:apiName/configs", resource.Configs)

			group.POST("/admin/register", p.AdminCtrl.Register)
			group.POST("/admin/reset-password", p.AdminCtrl.ResetPassword)

		}
	})
}
